﻿using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Security.Permissions;
using Basic4Net.Utility;


namespace Basic4Net.Controls
{
	/*－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－
	 * 自定义下拉框控件
	 * 功能说明：1、通过设置IsAddEmpty（true）设置下拉框是否添加空选择项。
	 *          2、通过设置IsAddAll来选择添加的空选择项是[Select]设为true,空选择项为[All]设为false
	 *          3、通过给EnumType赋值来绑定枚举值
	 －－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－*/
	[ToolboxData("<{0}:BNDropDownList runat=\"server\"> </{0}:BNDropDownList>")]
	public class BNDropDownList : DropDownList
	{
		#region 重写基类方法
		/// <summary>
		/// 添加空选择项
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			if (this.blAddEmpty)
			{
				if (this.blAddAll)
				{
					this.Items.Remove(new ListItem("[All]", ""));
					this.Items.Insert(0, new ListItem("[All]", ""));
				}
				else
				{
					this.Items.Remove(new ListItem("[Select]", ""));
					this.Items.Insert(0, new ListItem("[Select]", ""));
				}
			}
			base.OnInit(e);
		}

		/// <summary>
		/// 添加空选择项
		/// </summary>
		public override void DataBind()
		{

			base.DataBind();
			if (this.blAddEmpty)
			{
				if (this.blAddAll)
				{
					this.Items.Remove(new ListItem("[All]", ""));
					this.Items.Insert(0, new ListItem("[All]", ""));
				}
				else
				{
					this.Items.Remove(new ListItem("[Select]", ""));
					this.Items.Insert(0, new ListItem("[Select]", ""));
				}
			}
		}

		/// <summary>
		/// 输出控件
		/// </summary>
		/// <param name="writer"></param>
		protected override void Render(HtmlTextWriter writer)
		{
			base.Render(writer);
			if (this.blValEmpty)
			{
				writer.Write("<br/>");
				this.ctrValEmpty.RenderControl(writer);
			}
		}

		/// <summary>
		/// 重写list控件使之能添加子控件
		/// </summary>
		/// <returns></returns>
		protected override ControlCollection CreateControlCollection()
		{
			return new ControlCollection(this);
		}

		/// <summary>
		/// 添加非空验证控件
		/// </summary>
		protected override void CreateChildControls()
		{
			if (!ChildControlsCreated)
			{
				if (this.blValEmpty)
				{
					this.ctrValEmpty.ControlToValidate = this.ID;
					this.ctrValEmpty.Display = ValidatorDisplay.Dynamic;
					this.ctrValEmpty.ValidationGroup = this.ValidationGroup;

					this.ctrValEmpty.ID = "ValEmpty" + this.ID;
					if (this.ctrValEmpty.ErrorMessage.Equals(string.Empty))
					{
						this.ctrValEmpty.ErrorMessage = "Please specify the " + this.strValName;
					}
					this.Controls.Add(this.ctrValEmpty);
				}
			}
		}

		#endregion

		#region --- 非空验证控件
		private RequiredFieldValidator ctrValEmpty = new RequiredFieldValidator();
		private string strValName = "";
		/// <summary>
		/// 验证名
		/// </summary>
		[Category("ValidatorEmpty"), Description("验证名")]
		public string ValName
		{
			get { return this.strValName; }
			set { this.strValName = value; }
		}

		private bool blValEmpty = false;
		/// <summary>
		/// 是否非空验证
		/// </summary>
		[Category("ValidatorEmpty"), Description("是否非空验证")]
		public bool IsValEmpty
		{
			get { return this.blValEmpty; }
			set { this.blValEmpty = value; }
		}

		/// <summary>
		/// 自定义非空验证消息
		/// </summary>
		[Category("ValidatorEmpty"), Description("自定义非空验证消息")]
		public string ValEmptyMsg
		{
			get { return this.ctrValEmpty.ErrorMessage; }
			set { this.ctrValEmpty.ErrorMessage = value; }
		}
		#endregion

		#region --- 扩展属性
		private bool blAddEmpty = true;
		/// <summary>
		/// 是否添加空选择项
		/// </summary>
		[Category("CustProperty"), Description("是否添加空选择项")]
		public bool IsAddEmpty
		{
			get
			{
				return this.blAddEmpty;
			}
			set
			{
				this.blAddEmpty = value;
			}
		}

		private bool blAddAll = false;
		/// <summary>
		/// 空选择项为[All]设为true,空选择项为[Select]设为false
		/// </summary>
		[Category("CustProperty"), Description("空选择项为[All]设为true,空选择项为[Select]设为false")]
		public bool IsAddAll
		{
			get
			{
				return this.blAddAll;
			}
			set
			{
				this.blAddAll = value;
			}
		}

		/// <summary>
		/// 下拉框绑定的枚举类型
		/// </summary>
		[Category("CustProperty"), Description("下拉框绑定的枚举类型")]
		public Type EnumType
		{
			set
			{
				this._DataBind(TextAttributeTools.GetEnumTable(value).DefaultView, "Text", "Value");
			}
		}
		#endregion
	}
}
